#!/bin/bash

cd $(dirname "$0")

SELFDIR=`pwd`
CHROOT_DIR="$SELFDIR/nroot"

SUDO="sudo"

if [ "$UID" -eq 0 ] ; then
    SUDO=''
fi

if [ ! -d "$CHROOT_DIR" ] ; then
    $SUDO mkdir $CHROOT_DIR
    $SUDO chmod 755 $CHROOT_DIR
else
    $SUDO chown root:root $CHROOT_DIR
    $SUDO chmod 755 $CHROOT_DIR
fi

cd $CHROOT_DIR

DIRLIST="etc home usr tmp var root usr/local usr/bin usr/sbin var/www usr/lib usr/local/bin usr/share dev proc sys sys/fs sys/fs/cgroup"

for d in $DIRLIST ; do
    if [ ! -d "$d" ] ; then
        $SUDO mkdir $d
    fi
    $SUDO chown root:root $d
done

$SUDO cp $SELFDIR/ROOT_bashrc root/.bashrc
$SUDO cp $SELFDIR/HOME_profile root/.profile

if [ ! -L "lib" ] ; then
    $SUDO ln -s usr/lib lib
fi

if [ -L "/lib64" ] ; then 
    if [ ! -L "lib64" ] ; then
        $SUDO ln -s usr/lib lib64
    fi
else
    $SUDO mkdir lib64
fi

if [ -d "/usr/lib64" ] ; then
    if [ ! -d "usr/lib64" ] ; then
        $SUDO mkdir usr/lib64
    fi
fi

if [ ! -L "bin" ] ; then
    $SUDO ln -s usr/bin bin
fi

if [ ! -L "sbin" ] ; then
    $SUDO ln -s usr/sbin sbin
fi

FILE_CMD="cp mv ls touch rm rmdir mkdir ln chmod chown tar xz grep stat find pwd chgrp cat less diff diff3 wc uniq df du file less"
PROC_CMD="true false which whereis kill setsid bash"
NET_CMD="ping ss ip nmap route wget dig traceroute scp hostname"
OPT_CMDLIST="zip unzip gzip gunzip curl"
#OTH_CMD_LIST="date stty tr openssl sha256sum pam-auth-update pam_extrausers_chkpwd pam_extrausers_update pam_getenv pam_timestamp_check"
OTH_CMD_LIST="date stty tr openssl sha256sum sleep lscpu lsmem lspci"
#USER_CMD_LIST="su $SUDO_logsrvd $SUDO passwd nologin $SUDOedit groups"
USER_CMD_LIST="login gettty nologin groups"
CMDLIST="sort env dirname awk tee uname id wc whoami rev shasum printf git $OTH_CMD_LIST $FILE_CMD $PROC_CMD $NET_CMD $OPT_CMDLIST"

CUR_CMD_PATH=

> $SELFDIR/ldd_cmd_so

if which node ; then
    ldd `which node` >> $SELFDIR/ldd_cmd_so
fi

for c in $CMDLIST ; do
    CUR_CMD_PATH=`which $c`
    if [ -z "$CUR_CMD_PATH" ] ; then
        continue
    fi

    ldd $CUR_CMD_PATH >> $SELFDIR/ldd_cmd_so 2> /dev/null
    $SUDO cp $CUR_CMD_PATH ".$(dirname $CUR_CMD_PATH)"
    #case $c in
    #    passwd)
    #        chmod u+s ".$CUR_CMD_PATH"
    #        ;;
    #    nologin)
    #        chmod u+s ".$CUR_CMD_PATH"
    #        ;;
    #esac
done

echo '#!/bin/bash' > "usr/bin/clear"
echo 'printf "\033[2J\033[H"' >> "usr/bin/clear"

chmod +x "usr/bin/clear"

$SUDO ln -s bash bin/sh

ETC_LIST="debconf.conf default bash_completion bash_completion.d bash.bashrc pam.conf pam.d login.defs ssl ca-certificates.conf ca-certificates"

for f in $ETC_LIST ; do
    if [ -d "/etc/$f" ] ; then
        $SUDO cp -R /etc/$f etc/
    elif [ -f "/etc/$f" ] ; then
        $SUDO cp /etc/$f etc/
    fi
done

$SUDO cp $SELFDIR/etc/* -R etc/

$SUDO chown root:root -R etc/*

SHARE_LIST="bash-completion pam pam-configs"

for f in $SHARE_LIST ; do
    if [ -a "/usr/share/$f" ] ; then
        $SUDO cp -R /usr/share/$f ./usr/share
    fi
done

if [ ! -d "mno" ] ; then
    $SUDO git clone https://gitee.com/daoio/mno
else 
    cd mno && $SUDO git pull && cd ..
fi

$SUDO node $SELFDIR/mklib.js $SELFDIR/ldd_cmd_so

$SUDO chroot $SELFDIR/nroot bash -c 'cd mno && ./install.sh'

cd $CHROOT_DIR

MIN_DEV=`ls -l /dev/null | awk -F ' ' '{printf $6}'`
if [ ! -c "dev/null" ] ; then
    # 字符设备 主设备号（1表示内存） 从设备号
    # cat /proc/devices查看主设备类型和编号
    $SUDO mknod dev/null c 1 $MIN_DEV
    $SUDO chmod 666 dev/null
fi

MIN_DEV=`ls -l /dev/zero | awk -F ' ' '{printf $6}'`
if [ ! -c "dev/zero" ] ; then
    $SUDO mknod dev/zero c 1 $MIN_DEV
    $SUDO chmod 666 dev/zero
fi
